<?php
function mysql_max($tabulka, $sloupec)
{
    $query = $GLOBALS['db']->query("SELECT max($sloupec) FROM $tabulka;");
    while ($row = $query->fetch_array()) {
        return $row[0];
    }
}
echo '<div id="navigace"><a href="index.php?p=components&name=forum">Fórum Administrace</a> - Přidat obsah</div>';
if (isset($_POST['neodeslat_form'])) {
    echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
    exit();
} elseif (isset($_POST['nova_skupina'])) {
    $jmeno_skupiny = orez_retezec_2($_POST['jmeno_skupiny']);
    if (!empty($jmeno_skupiny)) {
        $max = mysql_max("" . $GLOBALS['db_prefix'] . "forum_skupiny", "poradi") + 1;
        $query = $GLOBALS['db']->query("INSERT INTO " . $GLOBALS['db_prefix'] .
            "forum_skupiny SET poradi = '$max', jmeno = '$jmeno_skupiny';");
    }
    $log->write(8, 'com_forum', 'NOW()', 'User created new group', $_SESSION['id']);
    echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
} elseif (!empty($_GET['pridat_forum'])) {
    $id_skupiny = 1 * $_GET['pridat_forum'];
    if ($_GET['pridat'] == 1) {
        if ((empty($_POST['jmeno_fora'])) || (empty($_POST['popisek_fora']))) {
            $chyba = "<br /><b>Jedno z povinných polí nebylo vyplněno.</b><br />";
        } else {
            $jmeno_fora = orez_retezec_2($_POST['jmeno_fora']);
            $popisek_fora = orez_retezec_2($_POST['popisek_fora']);
            $query = $GLOBALS['db']->query("SELECT max(poradi) FROM " . $GLOBALS['db_prefix'] .
                "forum_fora WHERE skupina = '$id_skupiny';") or die($GLOBALS['db']->error());
            $row = $query->fetch_array();
            $max = $row[0] + 1;
            $query = $GLOBALS['db']->query("INSERT INTO " . $GLOBALS['db_prefix'] .
                "forum_fora SET poradi = '$max', jmeno = '$jmeno_fora', popis = '$popisek_fora', skupina = '$id_skupiny';");
            $log->write(8, 'com_forum', 'NOW()', 'User created new forum in group #' . $id_skupiny,
                $_SESSION['id']);
            echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
        }
    }
    $oznam = "<form action=\"index.php?p=components&name=forum&a=pridat_obsah&pridat_forum=" .
        $id_skupiny . "&pridat=1\" method=\"post\" class=\"nove_forum\">
                                <fieldset>
                                        <legend>Nové fórum</legend>" . $chyba .
        "
                                        <label>Jméno:</label><input type=\"text\" name=\"jmeno_fora\" " .
        $js_form . " /><br />
                                        <label>Popisek fóra (max. 255 znaků):</label><textarea name=\"popisek_fora\" cols=\"0\" rows=\"0\"></textarea><br />
                                        <input type=\"submit\" name=\"nove_forum\" value=\"Přidat\" class=\"odeslat\" /><br />
                                </fieldset>
                        </form>";
} elseif ((!empty($_GET['forum_posun'])) && (!empty($_GET['skupina_id'])) && (!
empty($_GET['forum_id'])) && (!empty($_GET['forum_poradi']))) {
    $kam = orez_retezec($_GET['forum_posun']);
    $poradi = 1 * $_GET['forum_poradi'];
    $skupina_id = 1 * $_GET['skupina_id'];
    $forum_id = 1 * $_GET['forum_id'];
    $query = $GLOBALS['db']->query("SELECT max(poradi) FROM " . $GLOBALS['db_prefix'] .
        "forum_fora WHERE skupina = '$skupina_id';");
    while ($row = $query->fetch_array()) {
        $max = $row[0];
        if (($kam == "nahoru") && ($poradi != 1)) {
            $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
                "forum_fora SET poradi = poradi - 1 WHERE (poradi = '$poradi' and skupina = '$skupina_id');");
            $poradi--;
            $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
                "forum_fora SET poradi = poradi + 1 WHERE (poradi = '$poradi' and id <> '$forum_id' and skupina = '$skupina_id');");
        } elseif (($kam == "dolu") && ($poradi != $max)) {
            $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
                "forum_fora SET poradi = poradi + 1 WHERE (poradi = '$poradi' and skupina = '$skupina_id');");
            $poradi++;
            $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
                "forum_fora SET poradi = poradi - 1 WHERE (poradi = '$poradi' and id <> '$forum_id' and skupina = '$skupina_id');");
        }
        echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
    }
} elseif (!empty($_GET['skupina_smazani'])) {
    $id = 1 * $_GET['skupina_smazani'];
    if ((!empty($_GET['smazat'])) && (isset($_POST['smazat_skupinu']))) {
        $smazat = 1 * $_GET['smazat'];
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_skupiny WHERE poradi = '$smazat' LIMIT 1;");
        $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
            "forum_skupiny SET poradi = poradi - 1 WHERE poradi > '$smazat';");
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_fora WHERE skupina = '$id';");
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_temata WHERE skupina = '$id';");
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_prispevky WHERE skupina = '$id';");
        $log->write(8, 'com_forum', 'NOW()', 'User deleted forum group #' . $id, $_SESSION['id']);
        echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
    } else {
        $query = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] .
            "forum_skupiny WHERE id = '$id'");
        while ($row = $query->fetch_array()) {
            $oznam = "<form action=\"index.php?p=components&name=&name=&name=forum&a=pridat_obsah&skupina_smazani=" .
                $row['id'] . "&smazat=" . $row['poradi'] . "\" method=\"post\"><fieldset><legend>Smazání skupiny</legend><label>Opravdu si přejete smazat skupinu <b>" .
                $row['jmeno'] . "</b> včetně všech fór a příspěvků?</label><input type=\"submit\" value=\"Ano\" name=\"smazat_skupinu\" /> <input type=\"submit\" value=\"Ne\" name=\"neodeslat_form\" /><br /><br /></fieldset></form>";
        }
    }
} elseif ((!empty($_GET['skupina_posun'])) && (!empty($_GET['skupina_id'])) && (!
empty($_GET['skupina_poradi']))) {
    $kam = orez_retezec($_GET['skupina_posun']);
    $poradi = 1 * $_GET['skupina_poradi'];
    $id = 1 * $_GET['skupina_id'];
    if (($kam == "nahoru") && ($poradi != 1)) {
        $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
            "forum_skupiny SET poradi = poradi - 1 WHERE poradi = '$poradi';");
        $poradi--;
        $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
            "forum_skupiny SET poradi = poradi + 1 WHERE (poradi = '$poradi' and id <> '$id');");
    } elseif (($kam == "dolu") && ($poradi != mysql_max("" . $GLOBALS['db_prefix'] .
    "forum_skupiny", "poradi"))) {
        $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
            "forum_skupiny SET poradi = poradi + 1 WHERE poradi = '$poradi';");
        $poradi++;
        $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
            "forum_skupiny SET poradi = poradi - 1 WHERE (poradi = '$poradi' and id <> '$id');");
    }
    echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
} elseif (!empty($_GET['forum_smazani'])) {
    $id = 1 * $_GET['forum_smazani'];
    $skupina_id = 1 * $_GET['skupina_id'];
    if ((!empty($_GET['smazat'])) && (isset($_POST['smazat_forum']))) {
        $smazat = 1 * $_GET['smazat'];
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_fora WHERE (poradi = '$smazat' and skupina = '$skupina_id') LIMIT 1;");
        $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
            "forum_fora SET poradi = poradi - 1 WHERE (poradi > '$smazat' and skupina = '$skupina_id');");
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_temata WHERE forum = '$id';");
        $query = $GLOBALS['db']->query("DELETE FROM " . $GLOBALS['db_prefix'] .
            "forum_prispevky WHERE forum = '$id';");
        $log->write(8, 'com_forum', 'NOW()', 'User deleted forum #' . $id, $_SESSION['id']);
        echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
    } else {
        $query = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] .
            "forum_fora WHERE id = '$id'");
        while ($row = $query->fetch_array()) {
            $oznam = "<form action=\"index.php?p=components&name=&name=&name=forum&a=pridat_obsah&forum_smazani=" .
                $row['id'] . "&skupina_id=" . $skupina_id . "&smazat=" . $row['poradi'] . "\" method=\"post\"><fieldset><legend>Smazání fóra</legend><label>Opravdu si přejete smazat fórum <b>" .
                $row['jmeno'] . "</b> včetně všech témat a příspěvků?</label><input type=\"submit\" value=\"Ano\" name=\"smazat_forum\" /> <input type=\"submit\" value=\"Ne\" name=\"neodeslat_form\" /><br /><br /></fieldset></form>";
        }
    }
} elseif (!empty($_GET['skupina_editace'])) {
    $id_skupiny = 1 * $_GET['skupina_editace'];
    if ($_GET['zmen'] == 1) {
        if (empty($_POST['jmeno_skupiny'])) {
            $chyba = "<br /><b>Nové jméno nebylo vyplněno!</b><br />";
        } else {
            $jmeno_skupiny = orez_retezec_2($_POST['jmeno_skupiny']);
            $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
                "forum_skupiny SET jmeno = '" . $jmeno_skupiny . "' WHERE id = '" . intval($id_skupiny) .
                "';");
            echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
        }
    }
    $query = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] .
        "forum_skupiny WHERE id = '$id_skupiny';");
    while ($row = $query->fetch_array()) {
        $oznam = "<form action=\"index.php?p=components&name=forum&a=pridat_obsah&skupina_editace=" .
            $id_skupiny . "&zmen=1\" method=\"post\" class=\"nove_forum\">
                                <fieldset>
                                        <legend>Změna jména skupiny</legend>" .
            $chyba . "
                                        <label>Jméno:</label><input type=\"text\" name=\"jmeno_skupiny\" value=\"" .
            $row['jmeno'] . "\" " . $js_form . " /><br />
                                        <input type=\"submit\" name=\"zmena_skupiny\" value=\"Změnit\" class=\"odeslat\" /><br />
                                </fieldset>
                        </form>";
    }
} elseif (!empty($_GET['forum_editace'])) {
    $id_fora = 1 * $_GET['forum_editace'];
    if ($_GET['zmen'] == 1) {
        if ((empty($_POST['jmeno_fora'])) || (empty($_POST['popisek_fora']))) {
            $chyba = "<br /><b>Jedno z povinných polí nebylo vyplněno!</b><br />";
        } else {
            $jmeno_fora = orez_retezec_2($_POST['jmeno_fora']);
            $popisek_fora = orez_retezec_2($_POST['popisek_fora']);
            $query = $GLOBALS['db']->query("UPDATE " . $GLOBALS['db_prefix'] .
                "forum_fora SET jmeno = '$jmeno_fora', popis = '$popisek_fora' WHERE id = '$id_fora';");
            echo "<meta http-equiv=\"refresh\" content=\"1;index.php?p=components&name=forum&a=pridat_obsah\" />";
        }
    }
    $query = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] .
        "forum_fora WHERE id = '$id_fora';");
    while ($row = $query->fetch_array()) {
        $oznam = "<form action=\"index.php?p=components&name=forum&a=pridat_obsah&forum_editace=" .
            $id_fora . "&zmen=1\" method=\"post\" class=\"nove_forum\">
                                <fieldset>
                                        <legend>Změna údajů fóra</legend>" . $chyba .
            "
                                        <label>Jméno:</label><input type=\"text\" name=\"jmeno_fora\" value=\"" .
            $row['jmeno'] . "\" " . $js_form . " /><br />
                                        <label>Popisek fóra:</label><textarea name=\"popisek_fora\" " .
            $js_form . ">" . $row['popis'] . "</textarea><br />
                                        <input type=\"submit\" name=\"zmena_fora\" value=\"Změnit\" class=\"odeslat\" /><br />
                                </fieldset>
                        </form>";
    }
}
echo "\n\t<h3><b>Obsah fóra</b></h3>\n\t" . $oznam . "\n\t<div id=\"obsah\">\n";

$query = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] .
    "forum_skupiny ORDER BY poradi;");
while ($row = $query->fetch_array()) {
    echo "\t\t\t<table class=\"fora\" cellspacing=\"0\" cellpadding=\"0\">
                                <tr class=\"fora_hlavicka\">
                                        <td class=\"fora_hlavni\"><strong>" . $row['jmeno'] .
        "</strong></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&skupina_editace=" .
        $row['id'] . "\">editovat</a></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&skupina_smazani=" .
        $row['id'] . "\">smazat</a></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&skupina_posun=nahoru&skupina_id=" .
        $row['id'] . "&skupina_poradi=" . $row['poradi'] . "\">nahoru</a></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&skupina_posun=dolu&skupina_id=" .
        $row['id'] . "&skupina_poradi=" . $row['poradi'] . "\">dolů</a></td>
                                </tr>\n";
    $query2 = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['db_prefix'] .
        "forum_fora WHERE skupina = '" . $row['id'] . "' ORDER BY poradi;");
    $a = 1;
    while ($row2 = $query2->fetch_array()) {
    	
        $a++;
        if ($a % 2) {
            $tr_class = " class=\"tr_2\"";
        } else {
            $tr_class = "";
        }
        
        $td_class = " class=\"forum_forum\" ";
        echo "\t\t\t\t<tr" . $tr_class . ">
                                        <td".$td_class.">&brvbar;&nbsp;&nbsp;&nbsp;&nbsp;" . $row2['jmeno'] . "</td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&forum_editace=" .
            $row2['id'] . " \">editovat</a></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&forum_smazani=" .
            $row2['id'] . "&skupina_id=" . $row['id'] . "\">smazat</a></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&forum_posun=nahoru&skupina_id=" .
            $row['id'] . "&" . "forum_id=" . $row2['id'] . "&forum_poradi=" . $row2['poradi'] .
            "\">nahoru</a></td>
                                        <td align=\"center\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&forum_posun=dolu&skupina_id=" .
            $row['id'] . "&" . "forum_id=" . $row2['id'] . "&forum_poradi=" . $row2['poradi'] .
            "\">dolů</a></td>
                                </tr>";
    }

    echo "\t\t\t\t<tr class=\"fora_hlavicka\">
                                        <td colspan=\"5\"><a href=\"index.php?p=components&name=forum&a=pridat_obsah&pridat_forum=" .
        $row['id'] . "\">přidat nové fórum</a></td>
                                </tr>\n\t\t\t</table>\n";
}
?>
                        <form action="index.php?p=components&name=forum&a=pridat_obsah" method="post" id="nova_skupina">
                                <fieldset>
                                        <legend>Nová skupina</legend>
                                        <label>Jméno:</label><input type="text" name="jmeno_skupiny" <?php echo
$js_form; ?> /><br />
                                        <input type="submit" name="nova_skupina" value="Přidat" id="odeslat" /><br />
                                </fieldset>
                        </form>
        </div>